﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml.Linq;
using System.Diagnostics;

namespace ReporterApplication
{
    public partial class ImportProvision : Form
    {
        public ReportSettings Settings { get; set; }

        public ImportProvision()
        {
            InitializeComponent();
        }

        string cifFile = "D:\\ProvisionReports\\Result\\cif.xml";
        string imagePath = "D:\\ProvisionReports\\Result\\images\\";

        bool isStarted = false;

        private void importNotebookbutton_Click(object sender, EventArgs e)
        {
         
            importNotebookbutton.Enabled = false;

            isStarted = true;
            timer1.Enabled = true;
            progressBar1.Value = 5;

            if (File.Exists("pvwCIFLog.txt"))
                File.Delete("pvwCIFLog.txt");
            ////check temporary folder for cif and images
            var resultPath = Settings.CifFolder;

            if (!Directory.Exists(resultPath))
            {
                Directory.CreateDirectory(resultPath);
            }

            var tempPath = Path.Combine(resultPath, "temp");

            if (Directory.Exists(tempPath))
                Directory.Delete(tempPath, true);

            if (!Directory.Exists(tempPath))
            {
                Directory.CreateDirectory(tempPath);
            }

            imagePath = Path.Combine(resultPath, "images");

            if (Directory.Exists(imagePath))
                Directory.Delete(imagePath, true);

            if (!Directory.Exists(imagePath))
            {
                Directory.CreateDirectory(imagePath);
            }

            cifFile = Path.Combine(resultPath, "cif.xml");

            if (File.Exists(cifFile))
                File.Delete(cifFile);

            var provisionPath = provisionPathtextBox.Text;
            var provisionFile = Path.Combine(Path.Combine(provisionPath, "Program"), "PVWCIF.exe");

            
            //var provisionRequestFile = Path.Combine(Directory.GetCurrentDirectory(), "CIFRequest.xml");
            var provisionRequestFile = Path.Combine(resultPath, "CIFRequest.xml");

            string requestXml = @"<cifRequest images='true' layer='nested'>
    <target file='C:\Documents and Settings\Vavrik\Desktop\SOP Generator\Temp\CIF.xml'/>
    <ImageFormat>JPG</ImageFormat>
    <imageTarget>C:\Documents and Settings\Vavrik\Desktop\SOP Generator\Temp\</imageTarget>
</cifRequest>";

            File.WriteAllText(provisionRequestFile, requestXml);

            XDocument xd = XDocument.Load(provisionRequestFile);
            xd.Element("cifRequest").Element("target").Attribute("file").Value = cifFile;
            xd.Element("cifRequest").Element("imageTarget").Value = imagePath;

            xd.Save(provisionRequestFile);

            var pInfo = Process.Start(provisionFile, "request=\"" + provisionRequestFile + "\"");


        }

        bool cifFileReported = false;

        void Done()
        {
            MessageBox.Show("Provision Notebook Imported.", "Done",
         MessageBoxButtons.OK, MessageBoxIcon.Information);

            this.Close();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (isStarted)
            {
                if (!cifFileReported && File.Exists(cifFile))
                {
                    cifFileReported = true;

                    progressBar1.Value = 15;
                }

                if (Directory.Exists(imagePath))
                {
                    var filesCount = Directory.GetFiles(imagePath, "*.*").Count();
                    importedImageslabel.Text = filesCount.ToString();

                    if (filesCount > 0)
                    {
                        progressBar1.Value = 15 + filesCount / 10;
                    }
                }

                try
                {
                    using (FileStream fs = File.Open("pvwCIFLog.txt", FileMode.Open, FileAccess.ReadWrite, FileShare.None))
                    {
                        fs.Close();
                    }
                    progressBar1.Value = 100;

                    isStarted = false;
                    Done();
                }
                catch (Exception)
                {
                    
                }
            }
        }
    }
}
